package net.code.btalk.ui;

import net.code.btalk.BTalk;
import net.code.btalk.Buddy;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
import net.sourceforge.jxa.Jxa;

public class BuddyScreen extends MainScreen {
	public static BTalk btalk;
	public IconLabelField statusBanner;
	public BuddyListField buddyList;
	
	public BuddyScreen(BuddyListField l) {
		statusBanner = new IconLabelField(BuddyListField.onlineIcon, "Available");
		this.setTitle(statusBanner);

		this.buddyList = l;
		add(l);
		
		this.addMenuItem(new MenuItem("Chat", 0, 0) {
			public void run() {
				int idx = buddyList.getSelectedIndex();
				if (idx >= 0) {
					btalk.currentBuddy = (Buddy) buddyList.buddyVector.elementAt(idx);
					btalk.openBuddy(btalk.currentBuddy);
				}
			}
		});
		
		this.addMenuItem(new MenuItem("Retry", 0, 0) {
			public void run() {
				if (btalk.state == BTalk.STATE_FAILED) {
					btalk.state = BTalk.STATE_RETRYING;
					btalk.setMyStatus(BTalk.STATE_RETRYING, false, null);
					btalk.retryCount = 0;
					btalk.retryBtalk();
				}
			}
		});
		
		this.addMenuItem(new MenuItem("Options", 0, 0) {
			public void run() {
				btalk.pushScreen(new OptionScreen());
			}
		});
		
		// add exit menuitem
		this.addMenuItem(new MenuItem("Log out & Exit", 0, 0) {
			public void run() {
				if (btalk.state == BTalk.STATE_ONLINE) {
					btalk.logoffJxa();
				}
				btalk.exitBtalk();
			}
		});
		
		this.addMenuItem(new MenuItem("About", 0, 0) {
			public void run() {
				Dialog.inform("BTalk "+BTalk.Version+" (For 4.5)\n" +
								"By zhtlancer\n<zhtlancer@gmail.com>\n" +
								"You can follow @zhtlancer/btalk to keep track of this project, or visit the project site at google code.\n" +
								"This is a free software under GPL v2 License");
			}
		});
	}
	
	public boolean onClose() {
		if (btalk.state == BTalk.STATE_ONLINE ||
				btalk.state == BTalk.STATE_RETRYING ||
				btalk.state == BTalk.STATE_WAITING) {
			btalk.requestBackground();
		} else {
			btalk.exitBtalk();
		}
		return true;
	}
}
